<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/


if(isset($_POST['ajax'])) {
	define('IN', '1');
	define('REQUEST_AJAX', '1');
	include('../php/lib/common.php');
	include('../php/lib/panel_inc.php');
	connexionbdd();
		if(isset($_POST['mail']) && isset($_POST['nic'])) {
			$nic = intval(strtr($_POST['nic'], "-BW", ""));
			$mail = htmlentities(mysql_real_escape_string($_POST['mail']));
			if($nic == '0') {
				echo 2;
			}
			else {
				$nb = mysql_fetch_array(query("SELECT COUNT(*) AS verif FROM users WHERE u_nic='$nic' AND u_email='$mail'"));
				if($nb['verif'] == 1) {
					$chaine = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
					$nb_caract = 8;
					$pass = "";
					for($u = 1; $u <= $nb_caract; $u++) {
					    $nb = strlen($chaine);
					    $nb = mt_rand(0,($nb-1));
					    $pass.=$chaine[$nb];
					}
					$mpass = array();
					$mpass['{title}'] = '[Breizh héberg] Votre mot de passe Breizh héberg';
					$mpass['{contenu}'] = '
					Bonjour,<br />
					Voici votre nouveau mot de passe Breizh Héberg pour le compte <strong>'.$nic.'-BW</strong> :<br />
					Le mot de passe est : <strong>'.$pass.'</strong><br />
					Nous vous remercions de conserver celui-ci précieusement.';
					$message = str_replace(array_keys($mpass), array_values($mpass), file_get_contents("../tpl/mail-default.html"));
					$headers = "MIME-Version: 1.0\n";
					$headers .= "Content-type: text/html; charset=utf-8\n";
					$headers .= "From: Breizh Web Bot <robot@breizh-heberg.net>\n";
					$headers .= "Reply-to: Breizh Web Admin <admin@breizh-heberg.net>\n";
					if(mail($mail, $mpass['{title}'], $message, $headers)) {
						query("UPDATE users_password SET u_password='".sha1($pass)."', u_crypt='1' WHERE u_nic='".$nic."'");
						echo 1;
					}
					else {
						echo 3;
					}
				}
				else {
					echo 3;
				}
			}		
		}
		else {
			echo 3;
		}
	deconnexionbdd();
}
?>